
attach(COVID_Data_June_27_Treatments)

data9<- COVID_Data_June_27_Treatments



###### Q22

# prepare data
data9 <- corpus(COVID_Data_June_27_Treatments, text_field = 'Q22')
docvars(data9)$text <- texts(data9)
data9 <- dfm(data9, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "mask", "wear", "respond", "spread"),
             remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out9<-convert(data9, to = 'stm')

out9$meta$Negative <- as.factor(out9$meta$Negative)


##############How many topics? ##########################################

set.seed(5)
kResult9 <- searchK(out9$documents, out9$vocab, K = c(5,7, 10),
                    init.type = "Spectral", prevalence =~ Negative, data = out9$meta)

plot(kResult9)
par(mfrow = c(1, 1),mar = c(5, 5, 5, 5))
plot(kResult9$results$semcoh, kResult9$results$exclus, xlab = "Semantic Coherence",
     ylab = "Exclusivity")

text(kResult9$results$semcoh, kResult9$results$exclus, labels = paste("K",
                                                                      kResult9$results$K), pos = 1)

knitr::kable(kResult9$results)


#fit models and effect estimates

summary(out9)
data_10 <- stm(documents = out9$documents,
              vocab = out9$vocab,
              data = out9$meta,
              prevalence = ~ Negative,
              K = 5, verbose = FALSE)
prep_10 <- estimateEffect(1:5 ~ Negative, data_10,
                         meta = out9$meta, set.seed(2))

labelTopics(data_10, topics = NULL, n = 10)

par(mfrow = c(1, 1),mar = c(7, 1,3, .5))
plot(data_10, xlim=c(0,.7), n = 7, labeltype = "frex", topic.names = c("(1) Trump's Testing Response", "(2) Federal Government Response", "(3) President's Response", "(4) State Government", "(5) Public's Response"), custom.labels = "", text.cex = .7)

summary(prep_10)

###################### PLOTS #################################

par(mfrow = c(2, 3),mar = c(2.5, 1, 3, .5))
plot(prep_10, covariate="Negative", topics=c(1), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="Trump's Testing Response \n B = 0.01, SE = 0.008, p = 0.06",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.35), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10, covariate="Negative", topics=c(2), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="Federal Government Response \n B = 0.01, SE = 0.01, p = 0.18",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.35), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10, covariate="Negative", topics=c(3), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="President's Response \n B = -0.01, SE = 0.006, p = 0.04",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.27), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10, covariate="Negative", topics=c(4), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="State Government \n B = -0.004, SE = 0.009, p = 0.62",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.2), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10, covariate="Negative", topics=c(5), model=data_10, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="Public's Response \n B = -0.004, SE = 0.009, p = 0.54",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.2), labeltype ="custom", custom.labels=c('Negative', 'Positive'))




####################Find thoughts
attach(COVID_Data_June_27_Treatments)

data9r <- corpus(COVID_Data_June_27_Treatments, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond",  "mask", "wear", "spread"),
              remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out<-convert(data9r, to = 'stm')


model <- stm(out$documents, out$vocab, K = 5, max.em.its = 150, data = out$meta,
             init.type = "Spectral", prevalence =~ Negative)

summary(model)


thoughts1<- findThoughts(model, texts = out$meta$Q22, topics = 5, n = 35)

head(thoughts1)

























############Conservative Only (H5)

attach(COVID_Data_June_27_Treatments)

COVID_Data_June_27_Treatments_Conservative <- COVID_Data_June_27_Treatments[ which(Conservative=='1'),]


###### Q22

# prepare data
data9r <- corpus(COVID_Data_June_27_Treatments_Conservative, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "mask", "wear", "respond", "spread"),
             remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out9r<-convert(data9r, to = 'stm')

out9r$meta$Negative <- as.factor(out9r$meta$Negative)


##############How many topics? ##########################################

set.seed(5)
kResult9r <- searchK(out9r$documents, out9r$vocab, K = c(5,10, 7),
                    init.type = "Spectral", prevalence =~ Negative, data = out9r$meta)

plot(kResult9r)
par(mfrow = c(1, 1),mar = c(5, 5, 5, 5))
plot(kResult9r$results$semcoh, kResult9r$results$exclus, xlab = "Semantic Coherence",
     ylab = "Exclusivity")

text(kResult9r$results$semcoh, kResult9r$results$exclus, labels = paste("K",
                                                                      kResult9r$results$K), pos = 1)

knitr::kable(kResult9r$results)


#fit models and effect estimates

set.seed(9)
data_10r <- stm(documents = out9r$documents,
               vocab = out9r$vocab,
               data = out9r$meta,
               prevalence = ~ Negative,
               K = 5, verbose = FALSE)
prep_10r <- estimateEffect(1:5 ~ Negative, data_10r,
                          meta = out9r$meta)

labelTopics(data_10r, topics = NULL, n = 10)

par(mfrow = c(1, 1),mar = c(7, 1,3, .5))
plot(data_10r, xlim=c(0,1), n = 7, labeltype = "frex", topic.names = c("(1) President's Response", "(2) Media's Response", "(3) States' Response", "(4) Trump Administration's Response", "(5) President Trump's Response"), custom.labels = "", text.cex = .7)

summary(prep_10r)

###################### PLOTS #################################

par(mfrow = c(2, 3),mar = c(2.5, 1, 3, .5))
plot(prep_10r, covariate="Negative", topics=c(1), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="President's Reponse \n B = 0.04, SE = 0.01, p < 0.00",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.3), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10r, covariate="Negative", topics=c(2), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="Media's Response \n B = -0.06, SE = 0.01, p < 0.00",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.3), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10r, covariate="Negative", topics=c(3), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="States' Response \n B = -0.07, SE = 0.01, p < 0.00",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.32), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10r, covariate="Negative", topics=c(4), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="Trump Administration \n B = 0.02, SE = 0.01, p = 0.15",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.2), labeltype ="custom", custom.labels=c('Negative', 'Positive'))

plot(prep_10r, covariate="Negative", topics=c(5), model=data_10r, 
     method="pointestimate", cov.value1="0", cov.value2="1",
     xlab="", main="President Trump's Response \n B = 0.07, SE = 0.01, p < 0.00",cex.main=1.0, cex.lab=1.5, cex.axis=0.85,
     xlim=c(-.01,.33), labeltype ="custom", custom.labels=c('Negative', 'Positive'))




####################Find thoughts
attach(COVID_Data_June_27_Treatments_Conservative)

data9r <- corpus(COVID_Data_June_27_Treatments_Conservative, text_field = 'Q22')

docvars(data9r)$text <- texts(data9r)
data9r <- dfm(data9r, stem = TRUE, remove = c(stopwords(source = "smart"),"pandemic", "virus", "respond",  "mask", "wear", "spread"),
              remove_punct = TRUE) %>% dfm_trim(min_termfreq = 25)

out<-convert(data9r, to = 'stm')


model <- stm(out$documents, out$vocab, K = 5, max.em.its = 150, data = out$meta,
             init.type = "Spectral", prevalence =~ Negative)

summary(model)


thoughts1<- findThoughts(model, texts = out$meta$Q22, topics = 5, n = 35)

head(thoughts1)



